using System;
using System.Collections.Generic;
using System.Linq;
namespace WebClient.Controllers
{
	#region using
	using System.Web.Mvc;
	using WebClient.Models.ViewModels;
	using WebClient.Models;
	using WebClient.Models.Repositories;
	using WebClient.Helpers;
	using System.Web.Security;
    using WebClient.CF_DAL_WS;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectMenu()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_MENU
			};
			return View(CreateViewModel(data));
		}


		public ActionResult SelectMenuByIDRestaurant(int idrestaurant)
		{
			var data = new DataTransferViewModel
			{
				IDRestaurant = idrestaurant,
				EnumViewModelType = EnumViewModel.ADMIN_MENU,
			};
			return View("SelectMenu", CreateViewModel(data));
		}

		public ActionResult SelectMenuByIDRestaurantPaging(int idrestaurant)
		{
			var data = new DataTransferViewModel
			{
				IDRestaurant = idrestaurant,
				EnumViewModelType = EnumViewModel.ADMIN_MENU,
			};
			return PartialView("Templates/TH_ListMenu", CreateViewModel(data));
		}


		public ActionResult DeleteMenu(int id, int idrestaurant, int page = 1)
		{
			var result = _repMenu.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_MENU,
				CurrentPage = page,
                IDRestaurant = idrestaurant
			};
			return View("SelectMenu", CreateViewModel(data));
		}

		public ActionResult EditMenu(int id, int idrestaurant, int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_MENU,
				CurrentPage = page,
				IntID = id,
                IDRestaurant = idrestaurant
			};
			return PartialView("Templates/TH_EditMenu", CreateViewModel(data));
		}

		public ActionResult CancelEditingMenu(int idrestaurant, int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_MENU,
				CurrentPage = page,
                IDRestaurant = idrestaurant
			};
			return PartialView("Templates/TH_ListMenu", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateMenu(FormCollection forms)
		{
			string id = forms.GetValues("Menu_ID")[0];
			string name = forms.GetValues("Menu_Name")[0];
			var menu = _repMenu.SelectByID(int.Parse(id));
			menu.Name = name;
			var result = _repMenu.Update(menu);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_MENU,
                IDRestaurant = menu.IDRestaurant
			};
			return View("SelectMenu", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertMenu(FormCollection forms)
		{
			string idrestaurant = forms.GetValues("Menu_IDRestaurant")[0];
			string name = forms.GetValues("Menu_Name")[0];
			var newItem = new MenuDTO
			{
				IDRestaurant = int.Parse(idrestaurant),
				Name = name
			};
			var result = _repMenu.Insert(newItem);
			var data = new DataTransferViewModel
			{
				IDRestaurant = int.Parse(idrestaurant),
				EnumViewModelType = EnumViewModel.ADMIN_MENU,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectMenu", CreateViewModel(data));
		}


	}
}